﻿// RealSerialPort
// 
// Class representing a real serial port
// 
// FLMScan - Featured by FabLab Munich

using System;
using System.IO.Ports;

namespace FLMScan.Serial
{
    class RealSerialPort : ISerialPort
    {
        private static RealSerialPort instance;

        private SerialPort serialPort;
        private const string SerialPortNamePrefix = "COM";

        private RealSerialPort(int serialPortIndex)
        {
            serialPort = new System.IO.Ports.SerialPort();
            string comPortName = SerialPortNamePrefix + serialPortIndex;
            serialPort.PortName = comPortName;            
        }

        public static RealSerialPort Instance(int serialPortIndex)
        {
            if (instance == null)
                instance = new RealSerialPort(serialPortIndex);

            return instance;
        }
        
        public void Open()
        {
            serialPort.Open();
        }

        public void Write(char[] buffer, int offset, int count)
        {
            serialPort.Write(buffer, offset, count);
        }

        public void Close()
        {
            serialPort.Close();
        }
    }
}
